//
//  ASTeacherBasicInfoViewController.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/5/11.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASTeacherBasicInfoViewController.h"
#import "ASInfoCell.h"
#import "ASNetworkEngine.h"
#import "SVProgressHUD.h"


@interface ASTeacherBasicInfoViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *infoTableView;
@property (nonatomic, strong) NSArray     *infoArray;

@end



@implementation ASTeacherBasicInfoViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setLeftButtonWithImageName:@"ic_back" bgImageName:nil];
    self.strNavTitle = @"基本信息";
    [self setRightText:@"保存"];
    
    _infoArray = @[@"姓名", @"联系方式"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)initView
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    // infoTableView
    _infoTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _infoTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    _infoTableView.delegate = self;
    _infoTableView.dataSource = self;
    _infoTableView.backgroundColor = [UIColor whiteColor];
    _infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_infoTableView registerClass:[ASInfoCell class] forCellReuseIdentifier:NSStringFromClass([ASInfoCell class])];
    [bgView addSubview:_infoTableView];
}

- (void)rightButtonPressed:(id)action
{
    [self modifyBasicInfo];
}

- (void)modifyBasicInfo
{
    //修改用户信息接口 http://211.65.216.54/modifyUserByCode.action?userCode=xiaohu&userName=111&userMobile=15295749809&userEmail=601640129@qq.com&sex=M&birthday=1989-12-03&portraitNum=1&dormitory=1&political=1 sex: M表示男 F表示女 更新什么就传什么 dormitory宿舍 political政治面貌 （getPoliticalType.action这个接口中获取）
    
    // 联系方式
    ASInfoCell *teleCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];;
    NSString *userTele = teleCell.rightText.text;
    
    NSString *refer = @"modifyUserByCode.action?";
    NSString *prefix = kASHttpUrl(refer);
    NSString *userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString *totalUrl = [NSString stringWithFormat:@"%@userCode=%@&userMobile=%@",
                          prefix, userCode, userTele];
    
    [SVProgressHUD showWithStatus:@"处理中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            
            // 更新内存中的值
            [ASAppDelegate sharedAppDelegate].userInfo.userMobile = userTele;
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASInfoCell class])];
    
    ASInfoCell *infoCell = (ASInfoCell *)cell;
    
    [infoCell updateCellStatus:[_infoArray objectAtIndex:[indexPath row]]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


@end


